package com.youdianstar.app.ui.op

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.network.BaseApiLsStateObserver
import com.youdianstar.app.common.network.model.DataState
import com.youdianstar.app.BR
import com.youdianstar.app.R
import com.youdianstar.app.adapter.DeviceListAdapter
import com.youdianstar.app.databinding.OpDeviceListBinding
import com.youdianstar.app.entity.DeviceEntity

import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: DeviceListActivity
 * Author: 82149 Create: 2023/8/3 10:28
 * Changes (from 2023/8/3)
 * --------------------------------------------------
 * description: 设备列表
 * ---------------------------------------------------
 *
 */
class DeviceListActivity :BaseActivity<OpDeviceListBinding,OPMerchantViewModel>(){

    override fun layoutId(): Int = R.layout.op_device_list

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): OPMerchantViewModel {
        val model: OPMerchantViewModel by viewModel()
        return model
    }

    private var branchId = 0
    private val recordList : MutableList<DeviceEntity> = mutableListOf()
    private var mListAdapter: DeviceListAdapter?=null
    /*分页加载*/
    private var page = 0 //偏移量 0开始
    private var isRefresh : Boolean = true
    private var netSateCallback : NetStateCallback?=null

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this,getString(R.string.shop_myDevice))

        mBinding.rcyDevice.layoutManager = LinearLayoutManager(this)
        mListAdapter = DeviceListAdapter(this,recordList)
        mBinding.rcyDevice.adapter = mListAdapter

        mBinding.smartRefresh.run {
            this.setOnRefreshListener {
                refreshListView()
            }

            this.setOnLoadMoreListener {
                loadMoreList()
            }
        }
        netSateCallback = NetStateCallback(mBinding.rcyDevice,false)
        refreshListView(true)
    }

    override fun initClickEvent() {
        netSateCallback?.let { netCallback->
            mViewModel.deviceListLiveData.observe(this,netCallback)
        }
    }

    /**更新列表数据*/
    private fun refreshListView(isShow:Boolean = false){
        isRefresh = true
        page = 0
        if (isShow)
            showLoadingDialog("")
        mViewModel.getDeviceList(if (branchId == 0) null else branchId,offset = 0)
    }

    private fun loadMoreList(){
        isRefresh = false
        page = recordList.size
        mViewModel.getDeviceList(if (branchId == 0) null else branchId,offset = page)
    }

    /**更新列表布局数据*/
    fun updateListLayoutData(refresh:Boolean,netList: List<DeviceEntity>?){
        if (refresh)
            recordList.clear()
        netList?.let {
            recordList.addAll(it)
        }
        mListAdapter?.notifyDataSetChanged()
    }

    /**回调网络接口*/
    private fun onNetResult(isError:Boolean){
        if (isRefresh)
            mBinding.smartRefresh.finishRefresh()
        else
            mBinding.smartRefresh.finishLoadMore()
        if (recordList.isEmpty()){
            netSateCallback?.showViewState(if (isError) DataState.STATE_ERROR else DataState.STATE_EMPTY)
        }else{
            netSateCallback?.showViewState(DataState.STATE_SUCCESS)
        }
        dismissDialog()
    }

    inner class NetStateCallback(view: View, autoNet:Boolean): BaseApiLsStateObserver<List<DeviceEntity>>(view,autoNet){
        override fun onDataResult(data: List<DeviceEntity>?) {
            data?.let { list->
                updateListLayoutData(isRefresh,list)
            }
            onNetResult(false)
        }

        override fun onDataEmpty() {
            super.onDataEmpty()
            updateListLayoutData(isRefresh,null)
            onNetResult(false)
        }

        override fun onNetError(code: Int, e: String?) {
            super.onNetError(code, e)
            if (code!=-1) toastShow(e)
            onNetResult(code == -1)
        }

        override fun onReload(v: View?) {
            mViewModel.getDeviceList(if (branchId == 0) null else branchId,offset = page)
        }
    }
}